<script setup>
import { ref } from 'vue'
import { Edit, Delete } from '@element-plus/icons-vue'
import ChannelSelect from './components/ChannelSelect.vue'
import { artDelArtService, artGetListService, artGetListByCateIdService } from '@/api/article'
import { formatTime } from '@/utils/format'
import ArticleEdit from './components/ArticleEdit.vue'
import pageContainer from '@/components/PageContainer.vue'

const artList = ref([])
const isLoading = ref(false)
const params = ref({
  pagenum: 1, // 当前页码
  pagesize: 2, // 当前展示指定条数
  cate_id: '', // 文章分类id
  state: '' // 文章状态，已发布或草稿
})
const total = ref(0)
const articleEdit = ref(null)
const getList = async () => {
  isLoading.value = true
  const res = await artGetListService()
  // total.value = res.total
  artList.value = res.data
  isLoading.value = false
}
const handleSizeChange = (size) => {
  params.value.pagesize = size
  params.value.pagenum = 1
  getList()
}
const handleCurrentChange = (pageNum) => {
  params.value.pagenum = pageNum
  getList()
}

// 搜索 按钮
const onSearch = () => {
  // getList()
  artGetListByCateIdService(params.value.cate_id, params.value.state).then((res) => {
    artList.value = res.data
  })
  // 筛选 文章分类
  // 筛选 文章状态
}
const reset = () => {
  params.value.cate_id = ''
  params.value.state = ''
  getList()
}
getList()
const addArt = () => {
  articleEdit.value.open()
}
const editArt = (id) => {
  articleEdit.value.open(id)
}
const delArt = async (id) => {
  await ElMessageBox.confirm('你确认要删除这条信息吗', '温馨提示', {
    type: 'warning',
    confirmButtonText: '确认',
    cancelButtonText: '取消'
  })
  await artDelArtService(id)
  // 提示删除成功
  ElMessage.success({ message: '删除成功' })
  getList()
}
const success = (id) => {
  // 有id是编辑文章
  if (id) {
    getList()
    return
  }
  // 添加文章
  params.value.pagenum = Math.ceil((total.value + 1) / params.value.pagesize)
  getList()
}
</script>
<template>
  <page-container title="文章管理">
    <template #extra>
      <el-button type="primary" @click="addArt">添加文章</el-button>
    </template>
    <!-- 表单区域 -->
    <el-form inline>
      <ChannelSelect v-model="params.cate_id"></ChannelSelect>
      <el-form-item label="发布状态：">
        <el-select v-model="params.state">
          <el-option label="已发布" value="已发布"></el-option>
          <el-option label="草稿" value="草稿"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSearch">搜索</el-button>
        <el-button @click="reset">重置</el-button>
      </el-form-item>
    </el-form>
    <!-- 表格区域 -->
    <el-table :data="artList" v-loading="isLoading">
      <el-table-column label="文章标题">
        <template #default="{ row }">
          <el-link type="primary" :underline="false">{{ row.title }}</el-link>
        </template>
      </el-table-column>
      <el-table-column prop="cate_name" label="文章分类"></el-table-column>
      <el-table-column prop="pub_date" label="发表时间">
        <template #default="{ row }">
          {{ formatTime(row.pub_date) }}
        </template>
      </el-table-column>
      <el-table-column prop="state" label="状态"></el-table-column>
      <el-table-column label="操作">
        <template #default="{ row }">
          <el-button circle type="primary" @click="editArt(row.Id)" :icon="Edit" plain></el-button>
          <el-button circle type="danger" @click="delArt(row.Id)" :icon="Delete" plain></el-button>
        </template>
      </el-table-column>
      <template #empty>
        <el-empty description="没有数据"></el-empty>
      </template>
    </el-table>
    <!-- 分页 -->
    <el-pagination
      v-show="total > 0"
      v-model:current-page="params.pagenum"
      v-model:page-size="params.pagesize"
      style="margin-top: 10px; justify-content: flex-end"
      :page-sizes="[2, 4, 8, 10]"
      :background="true"
      layout="jumper, sizes, prev, pager, next, total"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
  </page-container>
  <ArticleEdit ref="articleEdit" @success="success"></ArticleEdit>
</template>
<style scoped>
.el-select {
  width: 214px;
}
</style>
